Public Class ctrFlags
    Public bMask As New BitMask
    Dim Setting As Boolean = False
    Public Event ValueChanged(ByVal sender As ctrFlags)
    Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles cList.ItemCheck
        If Not Setting Then
            bMask.ToggleBit(CInt((2 ^ (e.Index + 1)) / 2))
            RaiseEvent ValueChanged(Me)
        End If
    End Sub
    Public Sub SetItems()
        Setting = True
        For x As Integer = 0 To cList.Items.Count - 1
            cList.SetItemChecked(x, bMask.BitOn(CInt((2 ^ (x + 1)) / 2)))
        Next
        Setting = False
    End Sub
End Class
Public Class BitMask
    Dim mask As Integer
    Public Sub New()

    End Sub
    Public Sub New(ByVal num As Object)
        mask = num
    End Sub
    Public Function BitOn(ByVal num As Integer) As Boolean
        Dim nTest As Integer = mask And num
        If nTest = num Then
            Return True
        Else
            Return False
        End If
    End Function
    Public Sub SetBit(ByVal num As Integer)
        mask = mask Or num
    End Sub
    Public Sub ToggleBit(ByVal num As Integer)
        If mask And num = num Then
            mask = mask Xor num
        Else
            mask = mask Or num
        End If
    End Sub
    Public Property Value() As Integer
        Get
            Return mask
        End Get
        Set(ByVal newValue As Integer)
            mask = newValue
        End Set
    End Property
End Class
